package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @autahor: csl
 * @date: 2019/7/19
 * @description:
 * @version:1.0
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao dao;
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings==null&orderSettings.size()==0){
            throw new RuntimeException("数据不能为空");
        }
        for (OrderSetting orderSetting : orderSettings) {
            addMethod(orderSetting);
        }
    }

    public void addMethod(OrderSetting orderSetting) {
        //查询此日期是否存在
        long count=  dao.countByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            //有，更新数据
            dao.updateOrderSettingByOrderDate(orderSetting.getOrderDate());
        }else {
            //没有，添加数据
            dao.add(orderSetting);
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String startDate=date+"-1";
        String endDate=date+"-31";
        Map map=  new HashMap<>();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        List<OrderSetting>list= dao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Object> orderSetttingMap = new HashMap<>();
            orderSetttingMap.put("date",orderSetting.getOrderDate().getDate());
            orderSetttingMap.put("number",orderSetting.getNumber());
            orderSetttingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSetttingMap);
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        addMethod(orderSetting);
    }
}
